{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from MalardClient.MalardClient import MalardClient\n",
    "from MalardClient.DataSet import DataSet\n",
    "from MalardClient.BoundingBox import BoundingBox\n",
    "from MalardClient.MaskFilter import MaskFilter\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import json\n",
    "\n",
    "client = MalardClient()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Discover the parent data sets available in this environment\n",
    "pds = client.getParentDataSets()\n",
    "\n",
    "print(pds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#Query the available data sets for a given parent\n",
    "datasets = client.getDataSets('cryotempo')\n",
    "\n",
    "for ds in datasets:\n",
    "    print(str(ds))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Get the bounding box for each data set\n",
    "bounding_boxes = [client.boundingBox(ds) for ds in datasets]\n",
    "\n",
    "for bb in bounding_boxes:\n",
    "    print(str(bb))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#get the grid cells within a bounding box\n",
    "parentDs = 'cryotempo'\n",
    "ds = 'GRIS_BaselineC_Q2'\n",
    "region = 'greenland'\n",
    "\n",
    "dataset = DataSet(parentDs, ds, region )\n",
    "bb = client.boundingBox(dataset)\n",
    "\n",
    "gcs = client.gridCells( dataset, bb )\n",
    "\n",
    "print(gcs[0])\n",
    "\n",
    "shards = client.shards(dataset, bb )\n",
    "\n",
    "print(shards[0].shardName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#retrieve the data for the first grid cell\n",
    "\n",
    "resultInfo = client.executeQuery( dataset, gcs[0] )\n",
    "\n",
    "#Print the status of the request\n",
    "print( resultInfo.status )\n",
    "\n",
    "#create a dataframe from the results\n",
    "df = resultInfo.to_df\n",
    "\n",
    "#release the underlying server resources\n",
    "client.releaseCacheHandle( resultInfo.resultFileName )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#executeQuery polygon. Uses the shapefile passed in to determine the extent of the bounding box.\n",
    "#The projection of the shapefile needs to be consistent with the data that is passed in.\n",
    "#if using lat/lon for the xCol and yCol use EPSG: 4326 and (x,y) the proj4 code from the data set\n",
    "from datetime import datetime\n",
    "\n",
    "shapeFile = \"/data/puma1/scratch/mtngla/chucach/Chucach-polygon_XY.shp\"\n",
    "extentFilter=MaskFilter(p_shapeFile = shapeFile)\n",
    "\n",
    "alaskaDataSet = DataSet( 'mtngla', 'ADwithTDX', 'alaska' ) \n",
    "\n",
    "minT = datetime(2011,1,1,0,0)\n",
    "maxT = datetime(2011,2,1,0,0)\n",
    "\n",
    "chucachPolyInfo = client.executeQueryPolygon( alaskaDataSet, minT, maxT, extentFilter=extentFilter )\n",
    "\n",
    "chucachDf = chucachPolyInfo.to_df\n",
    "\n",
    "client.releaseCacheHandle(chucachPolyInfo.resultFileName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Applying a shapefile with a user defined bounding box\n",
    "\n",
    "print(chucachDf['x'].min())\n",
    "print(chucachDf['x'].max())\n",
    "print(chucachDf['y'].min())\n",
    "print(chucachDf['y'].max())\n",
    "\n",
    "bb = BoundingBox( -3100000, chucachDf['x'].max() , chucachDf['y'].min(), chucachDf['y'].max(), minT,maxT )\n",
    "\n",
    "bb_chucachInfo = client.executeQuery( alaskaDataSet, bb, maskFilters=extentFilter )\n",
    "\n",
    "bb_chucach_df = bb_chucachInfo.to_df\n",
    "\n",
    "client.releaseCacheHandle(bb_chucachInfo.resultFileName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "\n",
    "def simplePlot(df):\n",
    "## plot with no filters\n",
    "    plt.figure(figsize=(12,10))\n",
    "    plt.scatter(x=df['x'], y=df['y'], c=df['elev'], marker='.', s=20, vmax=7500)\n",
    "    plt.colorbar()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplePlot( chucachDf )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplePlot( bb_chucach_df )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#get the input swath file names from the input dataframe\n",
    "\n",
    "file_ids = df['swathFileId'].unique()\n",
    "\n",
    "results = client.getSwathNamesFromIds( dataset, file_ids )\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filterShards = client.shardsWithinPolygon( alaskaDataSet,minT,maxT, extentFilter=extentFilter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for s in filterShards:\n",
    "    print(s.shardName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shards = client.shards( alaskaDataSet,bb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for s in shards:\n",
    "    print(s.shardName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "###Jakobshavn bbox\n",
    "minX=-262187\n",
    "maxX=-93976\n",
    "minY=-2386746\n",
    "maxY=-2144979\n",
    "\n",
    "minT = datetime(2011,2,1,0,0)\n",
    "maxT = datetime(2011,3,1,0,0)\n",
    "\n",
    "wkt = \"POLYGON (( {} {}, {} {}, {} {}, {} {}, {} {}))\".format( minX,minY,minX,maxY,maxX,maxY,maxX,minY,minX,minY )\n",
    "\n",
    "greenland = DataSet(\"cryotempo\",\"GRIS_BaselineC_Q2\",\"greenland\")\n",
    "\n",
    "extentFilter = MaskFilter( p_wkt=wkt )\n",
    "\n",
    "gShards = client.shardsWithinPolygon(greenland, minT, maxT, extentFilter=extentFilter)\n",
    "\n",
    "for s in gShards:\n",
    "    print(s.shardName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "maskFilterIce = MaskFilter( p_shapeFile=\"/data/puma1/scratch/cryotempo/masks/icesheets.shp\"  )\n",
    "\n",
    "\n",
    "jakobshavnInfo = client.executeQueryPolygon( greenland, minT, maxT, extentFilter=extentFilter,maskFilters=maskFilterIce )\n",
    "\n",
    "jakobshavn = jakobshavnInfo.to_df\n",
    "\n",
    "client.releaseCacheHandle(jakobshavnInfo.resultFileName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplePlot( jakobshavn )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(jakobshavn.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
